package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import record.ProjectRecord;
import request.RequestFactory;
import request.RequestType;
import request.SaveRequest;
import system.Client;

/**
 * The project menu window.  The user will select forms on which they will
 * enter data
 * @author Stephan
 *
 */
@SuppressWarnings("serial")
public class ProjectMenuWindow extends JPanel implements ActionListener
{
	/**
	 * A checkmark icon
	 */
	private static final ImageIcon DONE = new ImageIcon("resources/images/done.png");
	
	/**
	 * The project title label
	 */
	private JLabel projectTitle;
	/**
	 * The label which will indicate that the building requirements are finished
	 */
	private JLabel buildingIcon;
	/**
	 * The label which will indicate that the geothermal requirements are finished
	 */	
	private JLabel geothermalIcon;
	/**
	 * The label which will indicate that the bore requirements are finished
	 */
	private JLabel boreIcon;
	
	/**
	 * The button which opens the building requirements window
	 */
	private JButton buildingRequirements;
	/**
	 * The button which opens the geothermal requirements window
	 */	
	private JButton geothermalRequirements;
	/**
	 * The button which opens the bore requirements window
	 */
	private JButton boreRequirements;
	/**
	 * The button that goes back to the project select screen
	 */
	private JButton backButton;
	/**
	 * The button that continues to the bore design window
	 */
	private JButton continueButton;
	
	/**
	 * The client through which this window will communicate with the server
	 */
	private Client client;
	/**
	 * The currently active project record
	 */
	private ProjectRecord record;
	/**
	 * The application currently rendering this window
	 */
	private Application app;
	
	private RequestFactory factory = new RequestFactory();
	
	/**
	 * Initializes the project window
	 * @param record the currently active project
	 * @param pApp the application rendering this window
	 */
	public ProjectMenuWindow(ProjectRecord record, Application pApp)
	{
		this.client = pApp.getClient();
		this.record = record;
		app = pApp;
		
		Container pane = this;
		
		projectTitle = new JLabel("<html><h1> " + record.getName());
		
		buildingIcon = new JLabel();
		geothermalIcon = new JLabel();
		boreIcon = new JLabel();
		
		buildingRequirements = new JButton("Building Requirements");
		geothermalRequirements = new JButton("Geothermal Requirements");
		boreRequirements = new JButton("Bore Requirements");
		backButton = new JButton("Back");
		continueButton = new JButton("Continue");
		
		buildingRequirements.addActionListener(this);
		geothermalRequirements.addActionListener(this);
		boreRequirements.addActionListener(this);
		backButton.addActionListener(this);
		continueButton.addActionListener(this);
		
		JPanel buildingPanel = new JPanel();
		JPanel geothermalPanel = new JPanel();
		JPanel borePanel = new JPanel();
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
		
		buildingPanel.add(buildingIcon);
		buildingPanel.add(buildingRequirements);
		
		geothermalPanel.add(geothermalIcon);
		geothermalPanel.add(geothermalRequirements);
	
		borePanel.add(boreIcon);
		borePanel.add(boreRequirements);
		
		buttonPanel.add(backButton);
		buttonPanel.add(continueButton);
		
		pane.setLayout(new BorderLayout());
		JPanel center = new JPanel(new GridLayout(3,1));
		center.add(buildingPanel);
		center.add(geothermalPanel);
		center.add(borePanel);
		
		pane.add(center, BorderLayout.CENTER);
		pane.add(buttonPanel, BorderLayout.PAGE_END);
		pane.add(projectTitle, BorderLayout.PAGE_START);
		
		processProjectData();
		app.getContentPane().removeAll();
		app.getContentPane().add(this);
		this.setVisible(true);
		app.setVisible(true);
	}

	/**
	 * Determines whether to add a check-mark icon to the project menu window
	 */
	private void processProjectData()
	{
		if (record.isBuildingRequirementsComplete())
			buildingIcon.setIcon(DONE);

		if (record.isGeothermalRequirementsComplete())
			geothermalIcon.setIcon(DONE);

		if (record.isBoreRequirementsComplete())
			boreIcon.setIcon(DONE);
	}

	/**
	 * @see ActionListener#actionPerformed(ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource().equals(buildingRequirements))
		{
			new BuildingRequirementsWindow(record, app); 
		}
		else if (arg0.getSource().equals(geothermalRequirements))
		{
			new GeothermalRequirementsWindow(record,app);
		}
		else if(arg0.getSource().equals(boreRequirements))
		{
			new BoreRequirementsWindow(record,app);
		}
		else if(arg0.getSource().equals(backButton))
		{
			try
			{
				SaveRequest save = (SaveRequest)factory.getRequest(RequestType.Save);
				save.setRecord(record);
				client.sendRequest(save);
			}
			catch (IOException e)
			{
				
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				
				e.printStackTrace();
			}
			new ProjectSelectionWindow(app);
		}
		else if(arg0.getSource().equals(continueButton))
		{
			if (record.isBuildingRequirementsComplete() && (record.isGeothermalRequirementsComplete())&& record.isBoreRequirementsComplete())
			{
				new GeothermalDesignWindow(record,app);
			}
			else
			{
				JOptionPane.showMessageDialog(this, "Please Complete All Data","Complete All Data",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
	}
	
	
}
